/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.model;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author natx
 *
 */
public class Products {
	
	Hashtable<String,Product> htProducts = new Hashtable<String,Product>();
	
	public String[] getStringArray() {
		String[] products = new String[htProducts.size()];
		
		Enumeration<Product> enuProducts = htProducts.elements();
		int i = 0;
		while ( enuProducts.hasMoreElements() ) {
			Product product = enuProducts.nextElement();
			products[i] = product.getReference();
			i++;
		}
		
		return products;
	}
	
	public Enumeration<Product> enumeration() {
		return htProducts.elements();
	}
	
	public void add(Product newProduct) throws ProductAlreadyExistsException {
		if ( htProducts.containsKey(newProduct.getReference()) ) throw new ProductAlreadyExistsException();
		htProducts.put(newProduct.getReference(),newProduct);
	}
	
	public void add(String reference, String description, String details, String supplier, double unitNetWeight, double unitGrossWeight, int unitsPerBox, Box box, int boxesPerLayer, int layersPerPallet, Pallet pallet, boolean Stackable) throws PalletAlreadyExistsException {
		if ( htProducts.containsKey(reference) ) throw new PalletAlreadyExistsException();
		htProducts.put(reference, new Product(reference, description, details, supplier, unitNetWeight, unitGrossWeight, unitsPerBox, box, boxesPerLayer, layersPerPallet, pallet, Stackable));
	}
	
	public Product getProduct(String reference) throws ProductDoesNotExistException {
		if ( !htProducts.containsKey(reference) ) throw new ProductDoesNotExistException();
		return htProducts.get(reference);
	}
	
	public boolean productExists(String reference) {
		return htProducts.containsKey(reference);
	}

}
